package com.top.mall.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.widget.FrameLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ScreenUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.top.mall.R
import com.top.mall.entity.LabelGoods
import kotlinx.android.synthetic.main.popup_goods_screen.view.*
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AlphaConfig
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.Direction
import razerdp.util.animation.TranslationConfig


class GoodsScreenPopup(
    context: Context, val onConfirm: (labelId: Int?) -> Unit
) : BasePopupWindow(context) {

    private val screenLabelAdapter =
        object : BaseQuickAdapter<LabelGoods, BaseViewHolder>(R.layout.item_goods_screen_label) {

            var selectedPosition = -1
                set(value) {
                    if (field != value) {
                        field = value
                        notifyDataSetChanged()
                    }
                }

            override fun convert(holder: BaseViewHolder, item: LabelGoods) {
                holder.setText(R.id.rvScreenLabelName, item.title)
                holder.itemView.isSelected = holder.layoutPosition == selectedPosition
            }
        }

    init {
//        setAlignBackground(true)
//        setAlignBackgroundGravity(Gravity.TOP)
//        setAlignBackgroundGravity(Gravity.LEFT)
//        setAlignBackgroundGravity(Gravity.START)
//        setMaxHeight(600)
//        setHeightAsAnchorView(true)

        isOutSideTouchable = true
        setBackground(0)
        height = ViewGroup.LayoutParams.MATCH_PARENT
//        offsetY = -20
        setWidthAsAnchorView(true)
        setPopupGravity(GravityMode.RELATIVE_TO_ANCHOR, Gravity.BOTTOM)
    }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_goods_screen)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.findViewById<View>(R.id.view1).setOnClickListener {
            this.dismiss()
        }
        contentView.findViewById<TextView>(R.id.tvScreenPopupReset).setOnClickListener {
            screenLabelAdapter.selectedPosition = -1
            onConfirm((null))
        }
        contentView.findViewById<TextView>(R.id.tvScreenPopupComplete).setOnClickListener {
            this.dismiss()
        }
    }

    fun fillData(labelGoods: MutableList<LabelGoods>, includeGoodsCateSort: View) {
        contentView.rvScreenPopupLabel.adapter = screenLabelAdapter
        screenLabelAdapter.setList(labelGoods)
        screenLabelAdapter.setOnItemClickListener { _, _, position ->
            screenLabelAdapter.selectedPosition = position
            onConfirm(screenLabelAdapter.getItem(position).id)
        }
//        val intArray: IntArray = IntArray(2)
//        includeGoodsCateSort.getLocationOnScreen(intArray)
//        LogUtils.e("${intArray[0]}=====${intArray[1]}=======${ScreenUtils.getAppScreenHeight()}")
//        height = ScreenUtils.getAppScreenHeight() - intArray[1] - ConvertUtils.dp2px(50f)
    }
}